package myJs.projectCps.boxplot

import cats.effect.SyncIO
import com.softwaremill.quicklens._
import japgolly.scalajs.react.vdom._
import japgolly.scalajs.react._
import myJs.cps._
import myJs.tool.Tool._

import scala.collection.immutable.SeqMap
import myJs.myPkg.Hooks._
import japgolly.scalajs.react.React.Fragment
import shared.pojo.mission._

/** Created by yz on 3/11/2020
 */
object LayoutCp {

  case class Props(
    basicData: Cp.plotO.LayoutData,
    basicDataChange: (Cp.plotO.LayoutData => Cp.plotO.LayoutData) => SyncIO[Unit],
    allData:Cp.plotO.Request
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>

    object FTool {

      def vChange[T](f: PathLazyModify[Cp.plotO.LayoutData, T])(g: String => T) = (v: String) => {
        props.basicDataChange {
          f.setTo(g(v))
        }
      }

      def strChange(f: PathLazyModify[Cp.plotO.LayoutData, String]) = FTool.vChange(f)(x=>x)

    }

    val showSampleMap = SeqMap("T" -> messages("yes"), "F" -> messages("no"))
    val basicData = props.basicData
    val plotSignificantMap=SeqMap(
      "F"->messages("no"),
      "comparisons"->"comparisons",
      "post_hoc"->"post_hoc",
    )
    val isMul=props.allData.color.groups.size>2

    Fragment(
      SelectInlineCp.Props(
        value = basicData.plotSignificant,
        label = "绘制两两之间显著差异",
        name = s"plotSignificant",
        optionMap = plotSignificantMap,
        onChange = FTool.strChange(modifyLens[Cp.plotO.LayoutData](_.plotSignificant)),
        labelWidth = 4,
        inputWidth = 4,
        show = isMul
      ).render,
      SelectInlineCp.Props(
        value = basicData.plotPoint,
        label = "是否绘制数据点",
        name = s"spot",
        optionMap = showSampleMap,
        onChange = FTool.strChange(modifyLens[Cp.plotO.LayoutData](_.plotPoint)),
        labelWidth = 4,
        inputWidth = 4
      ).render,
      InputInlineCp.Props(
        show=(basicData.plotPoint=="T"),
        label = "数据点透明度(0-1)",
        name = "jitterAlpha",
        labelWidth = 4,
        inputWidth = 8,
        value = basicData.jitterAlpha,
        onChange = FTool.strChange(modifyLens[Cp.plotO.LayoutData](_.jitterAlpha)),
      ).render,
      InputInlineCp.Props(
        show=(basicData.plotPoint=="F"),
        label = "离群点透明度(0-1)",
        name = "outlierAlpha",
        labelWidth = 4,
        inputWidth = 8,
        value = basicData.outlierAlpha,
        onChange = FTool.strChange(modifyLens[Cp.plotO.LayoutData](_.outlierAlpha)),
      ).render,
      InputInlineCp.Props(
        label = "盒子宽度(0-1)",
        name = "boxWidth",
        labelWidth = 4,
        inputWidth = 8,
        value = basicData.boxWidth,
        onChange = FTool.strChange(modifyLens[Cp.plotO.LayoutData](_.boxWidth)),
      ).render,
      BetweenInlineCp.Props(
        label = messages("picSizeWidthHeight"),
        startName = "width",
        start = basicData.width,
        startChange = FTool.strChange(modifyLens[Cp.plotO.LayoutData](_.width)),
        endName = "height",
        end = basicData.height,
        endChange = FTool.strChange(modifyLens[Cp.plotO.LayoutData](_.height))
      ).render,
    )


  }

}
